package info.javatech.jmf.client.wrappers;

import info.javatech.jmf.client.controller.JMFAbstractComponentWrapper;
import info.javatech.jmf.client.controller.exceptions.ComponentConstructionException;
import info.javatech.jmf.client.controller.wsclient.PCommand;
import info.javatech.jmf.client.controller.wsclient.PComponent;
import info.javatech.jmf.client.controller.wsclient.PEvent;
import info.javatech.jmf.client.controller.wsclient.PItem;
import info.javatech.jmf.client.util.InvalidComponentException;
import info.javatech.jmf.client.util.PComponentUtils;
import info.javatech.jmf.common.ProtocolConstants;
import info.javatech.jmf.common.components.TextFieldConstants;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author afedotov
 */
public class TextFieldWrapper extends JMFAbstractComponentWrapper{
    
    /** Creates a new instance of TextFieldWrapper */
    public TextFieldWrapper() {
    }
    
    public PEvent handleEvent(Displayable displayable, Item Item, Command command) {
        return new PEvent(getController().askToPersist(command),ProtocolConstants.EVENT_KIND_COMMAND);
    }
    
    
    
    public PComponent persistImplementation(Object obj, PComponent eventSource) {
        PComponent retValue = new PItem();
        
        retValue.setPropertyNames(new String[]{TextFieldConstants.STRING_PROPERTY_NAME});
        retValue.setPropertyValues(new String[]{((TextField)obj).getString()});
        
        retValue.setType(TextFieldConstants.TYPE_NAME);
        
        return retValue;
    }
    
    public Object constructInstanceImplementation(PComponent pc) throws ComponentConstructionException {
        TextField textField;
        try {
            
            String label = PComponentUtils.getStringProperty(pc, TextFieldConstants.LABEL_PROPERTY_NAME);
            String string = PComponentUtils.getStringProperty(pc, TextFieldConstants.STRING_PROPERTY_NAME);
            int maximumSize = PComponentUtils.getIntProperty(pc, TextFieldConstants.MAXIMUMSIZE_PROPERTY_NAME);
            int constrains = PComponentUtils.getIntProperty(pc, TextFieldConstants.CONSTRAINS_PROPERTY_NAME);
            
            textField = new TextField(label,string,maximumSize,constrains);
            
            textField.setInitialInputMode(PComponentUtils.getStringProperty(pc,TextFieldConstants.INITIALINPUTMODE_PROPERTY_NAME));
            textField.setPreferredSize(
                    PComponentUtils.getIntProperty(pc,TextFieldConstants.PREFFEREDHSIZE_PROPERTY_NAME),
                    PComponentUtils.getIntProperty(pc,TextFieldConstants.PREFFEREDVSIZE_PROPERTY_NAME));
            
            PCommand[] commands = ((PItem)pc).getCommands();
            
            for(int i = 0;i<commands.length;i++) {
                Command command = (Command) getController().askToConstruct(commands[i]);
                textField.addCommand(command);
                if (command.getLabel().equals(PComponentUtils.getStringProperty(pc,TextFieldConstants.DEFAULTCOMMAND_PROPERTY_NAME)))
                    textField.setDefaultCommand(command);
            }
            
            return textField;
            
        } catch (InvalidComponentException ex) {
            throw new ComponentConstructionException("Following error occured during component construction:"+ex.getMessage());
        }
    }
    
}
